package com.yonyou.common.service.messagecenter.rabbitmq.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yonyou.common.service.messagecenter.dao.TtMessageHistoryConsumerMapper;
import com.yonyou.common.service.messagecenter.rabbitmq.entity.MessageProtocol;
import com.yonyou.common.service.messagecenter.rabbitmq.entity.MessageProtocolDto;
import com.yonyou.common.service.messagecenter.rabbitmq.entity.TtMessageHistory;
import com.yonyou.common.service.messagecenter.rabbitmq.entity.TtMessageHistoryConsumer;
import com.yonyou.common.service.messagecenter.rabbitmq.entity.TtResendMessage;
import com.yonyou.common.service.messagecenter.util.MqConsts;

import tk.mybatis.mapper.entity.Example;

@Service
public class MessageHistoryConsumerService {
	
	@Autowired
	private TtMessageHistoryConsumerMapper historyMapper;

    @Transactional(rollbackFor=Exception.class)
    public void insertHistory(TtMessageHistoryConsumer record){
    	historyMapper.insert(record);
    }

    @Transactional(rollbackFor=Exception.class)
    public void updateMessageHis(TtMessageHistoryConsumer record,String status,String msg){
    	record.setStatus(status);
    	record.setUpdateDate(new Date(System.currentTimeMillis()));
    	if(msg.length()>1500){
        	record.setAppReturnMessage(msg.substring(0, 1500));
    	}else{
        	record.setAppReturnMessage(msg);
    	}
/*    	if(status.equals(MqConsts.CONSUMER_STATUS_FAIL))
    		record.setTryCount(record.getTryCount()+1);*/
    	historyMapper.updateByPrimaryKeySelective(record);
    }
    
    public TtMessageHistoryConsumer findSuccessMessageHistory(String sign,String endPointName){
    	Example example = new Example(TtMessageHistoryConsumer.class);
    	example.createCriteria().andCondition("sign=", sign).andCondition("consumer=", endPointName).andCondition("status=", MqConsts.CONSUMER_STATUS_FINISH);
    	List<TtMessageHistoryConsumer> records=historyMapper.selectByExample(example);
    	if(records.size()==0)
    		return null;
    	return records.get(0);
    }
    public TtMessageHistoryConsumer newMessageHistory(MessageProtocolDto mes){
    	TtMessageHistoryConsumer record =new TtMessageHistoryConsumer();
    	record.setCreateDate(new Date(System.currentTimeMillis()));
    	record.setStatus(MqConsts.CONSUMER_STATUS_START);
    	record.setSign(mes.getSign());
    	record.setAppid(mes.getAppid());
    	record.setMessageid(MessageProtocol.parseMessageId(mes.getData()));
    	record.setConsumer(mes.getEndPointName());
    	//record.setTryCount(0);
    	return record;
    }
    
    
    /**
     * 消费记录分页查询
     * 
     * @param page
     * @param pageSize
     * @param messageContent
     * @param sign
     * @return
     */
    public PageInfo<TtMessageHistoryConsumer> getAllConsumersByPage(int page,int pageSize,String sign){
    	Example example = new Example(TtMessageHistoryConsumer.class);
    	example.createCriteria()
    			.andCondition(StringUtils.isEmpty(sign)?"1=1":"sign='"+sign+"'");
    	example.orderBy("id").desc(); 
    	PageInfo<TtMessageHistoryConsumer> pages= PageHelper.startPage(page, pageSize).doSelectPageInfo(() -> historyMapper.selectByExample(example));
    	
    	return pages;
    }
}
